package com.cq.hd.rabbit.consumer;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.constant.RabbitMqConstant;
import com.cq.hd.common.entity.mq.UserLoginRecordMsg;
import com.cq.hd.member.api.TbUserLoginRecordApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
@RabbitListener(queues = RabbitMqConstant.USER_LOGIN_RECORD_QUEUE)
public class UserLoginRecordMsgConsumer {

    @Autowired
    private TbUserLoginRecordApi userLoginRecordApi;

    @RabbitHandler
    public void receiveMsg(Map map) {
        log.info("userLoginRecordMsg消息：{}", map.toString());
        String messageData = (String) map.get("messageData");
        UserLoginRecordMsg userLoginRecordMsg = JSON.parseObject((String) map.get("messageData"), UserLoginRecordMsg.class);

        try {
            userLoginRecordApi.saveUserLoginRecord(userLoginRecordMsg);
        } catch (Exception e) {
            log.error("消费userLoginRecordMsg消息失败，userId：{}", userLoginRecordMsg.getUserId(), e);
        }
    }

}
